package com.kx.center.task;

import java.util.HashMap;
import java.util.List;

import org.apache.commons.lang.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Service;

import com.kx.center.entity.Trade;
import com.kx.center.entity.huiyin.Merchant;
import com.kx.center.entity.huiyin.PayTranLog;
import com.kx.center.entity.huiyin.SaleOrder;
import com.kx.center.entity.huiyin.SaleOrderPay;
import com.kx.center.service.TradeService;
import com.kx.center.util.HttpClientUtil;
import com.kx.center.util.JSONParser;
import com.kx.center.util.StringUtil;

@Service
public class TransferTradeTask {

	public static void main(String[] args) {
		ApplicationContext ac = new ClassPathXmlApplicationContext(
				"applicationContext.xml");
		TransferTradeTask t = ac.getBean(TransferTradeTask.class);
		t.execute();
	}

	@Autowired
	private TradeService tradeService;

	public void execute() {

		System.out.println("订单数据开始对接");
		// 获取erp_sale_order_pay表
		try {
			Long l = null;
			if (tradeService.saleOrderPayMaxTime() == null) {
				l = 1446307200000L;
			} else {
				l = tradeService.saleOrderPayMaxTime();
			}
			HashMap<String, String> opts = new HashMap<String, String>();
			opts.put("mid", "100100210000038");
			opts.put("lastReceivedTime", l.toString());
			opts.put("count", "10000");
			opts.put("tableName", "erp_sale_order_pay");
			String data = JSONParser.stringify(opts);
			String url = "http://cashier.91huishang.com//wizarposOpen-server/v1_0/saleData/getdata";

			String json = HttpClientUtil.post(url, data);
			@SuppressWarnings("unchecked")
			HashMap<String, Object> hashmap = (HashMap<String, Object>) JSONParser
					.parse(json, new HashMap<String, Object>().getClass());
			@SuppressWarnings("unchecked")
			List<HashMap<String, Object>> list = (List<HashMap<String, Object>>) hashmap
					.get("saleOrderPayList");
			for (HashMap<String, Object> map : list) {
				SaleOrderPay saleOrderPay = new SaleOrderPay();
				saleOrderPay.setId(map.get("id").toString().trim());
				saleOrderPay.setOrderId(map.get("order_id").toString().trim());
				saleOrderPay.setPayMode(NumberUtils.toInt(map.get("pay_mode")
						.toString().trim()));
				saleOrderPay.setDisAmount(NumberUtils.toDouble(map
						.get("dis_amount").toString().trim()) / 100);
				saleOrderPay.setPayAmount(NumberUtils.toDouble(map
						.get("pay_amount").toString().trim()) / 100);
				saleOrderPay.setOddAmount(NumberUtils.toDouble(map
						.get("odd_amount").toString().trim()) / 100);
				saleOrderPay.setPayDesc(map.get("pay_desc").toString().trim());
				saleOrderPay.setLastTime(NumberUtils.toLong(map
						.get("last_time").toString().trim()));
				saleOrderPay.setDisCount(NumberUtils.toInt(map.get("dis_count")
						.toString().trim()));
				tradeService.insertSaleOrderPay(saleOrderPay);
			}
		} catch (Exception e) {
			System.out.println(e);
		}

		// 获取erp_sale_order表
		try {
			Long l = null;
			if (tradeService.saleOrderMaxTime() == null) {
				l = 1446307200000L;
			} else {
				l = tradeService.saleOrderMaxTime();
			}
			HashMap<String, String> opts = new HashMap<String, String>();
			opts.put("mid", "100100210000038");
			opts.put("lastReceivedTime", l.toString());
			opts.put("count", "10000");
			opts.put("tableName", "erp_sale_order");
			String data = JSONParser.stringify(opts);
			String url = "http://cashier.91huishang.com//wizarposOpen-server/v1_0/saleData/getdata";

			String json = HttpClientUtil.post(url, data);
			@SuppressWarnings("unchecked")
			HashMap<String, Object> hashmap = (HashMap<String, Object>) JSONParser
					.parse(json, new HashMap<String, Object>().getClass());
			@SuppressWarnings("unchecked")
			List<HashMap<String, Object>> list = (List<HashMap<String, Object>>) hashmap
					.get("saleOrderList");
			for (HashMap<String, Object> map : list) {
				SaleOrder saleOrder = new SaleOrder();
				saleOrder.setId(map.get("id").toString().trim());
				saleOrder.setSn(map.get("sn").toString().trim());
				saleOrder.setOrderNo(map.get("order_no").toString().trim());
				saleOrder.setStorageId(map.get("storage_id").toString().trim());
				saleOrder.setOrderTime(NumberUtils.toLong(map.get("order_time")
						.toString().trim()));
				saleOrder.setAmount(NumberUtils.toDouble(map.get("amount")
						.toString().trim()) / 100);
				saleOrder.setOrderFlag(NumberUtils.toInt(map.get("order_flag")
						.toString().trim()));
				saleOrder.setOrderId(StringUtil
						.checkString((String) map.get("order_id")).toString()
						.trim());
				saleOrder.setRemark(StringUtil
						.checkString((String) map.get("remark")).toString()
						.trim());
				saleOrder.setLastTime(NumberUtils.toLong(map.get("last_time")
						.toString().trim()));
				saleOrder.setOrderSource(map.get("order_source").toString()
						.trim());
				saleOrder.setPayStatus(StringUtil
						.checkString((String) map.get("pay_status")).toString()
						.trim());
				saleOrder.setIsRevokeThanLog(StringUtil
						.checkString((String) map.get("is_revoke_than_log"))
						.toString().trim());
				saleOrder.setMasterTranLogId(StringUtil
						.checkString((String) map.get("master_tran_log_id"))
						.toString().trim());
				saleOrder.setOfflineTranLogId(StringUtil
						.checkString((String) map.get("offline_tran_log_id"))
						.toString().trim());
				tradeService.insertSaleOrder(saleOrder);
			}
		} catch (Exception e) {
			System.out.println(e);
		}

		// 获取mrt_merchant_card表
		try {
			HashMap<String, String> opts = new HashMap<String, String>();
			opts.put("mid", "100100210000038");
			opts.put("lastReceivedTime", "1446307200000");
			opts.put("count", "10000");
			opts.put("tableName", "mrt_merchant_card");
			String data = JSONParser.stringify(opts);
			String url = "http://cashier.91huishang.com//wizarposOpen-server/v1_0/saleData/getdata";

			String json = HttpClientUtil.post(url, data);
			@SuppressWarnings("unchecked")
			HashMap<String, Object> hashmap = (HashMap<String, Object>) JSONParser
					.parse(json, new HashMap<String, Object>().getClass());
			@SuppressWarnings("unchecked")
			List<HashMap<String, Object>> list = (List<HashMap<String, Object>>) hashmap
					.get("merchantCardList");
			List<String> idList = tradeService.findMerchantIdList();
			for (HashMap<String, Object> map : list) {
				if(!idList.contains(map.get("id").toString().trim())){
					Merchant merchant = new Merchant();
					merchant.setId(map.get("id").toString().trim());
					merchant.setCreateTime(NumberUtils.toLong(map
							.get("create_time").toString().trim()));
					merchant.setCardNo(NumberUtils.toLong(map.get("card_no")
							.toString().trim()));
					merchant.setLastTime(NumberUtils.toLong(map.get("last_time")
							.toString().trim()));
					merchant.setUserName(map.get("username").toString().trim());
					tradeService.insertMerchant(merchant);
				}else{
					Merchant merchant = new Merchant();
					merchant.setId(map.get("id").toString().trim());
					merchant.setLastTime(NumberUtils.toLong(map.get("last_time")
							.toString().trim()));
					merchant.setUserName(map.get("username").toString().trim());
					tradeService.updateMerchant(merchant);
				}
			}
		} catch (Exception e) {
			System.out.println(e);
		}
		
		//获取pay_tran_log表
		try {
			Long l = null;
			if (tradeService.payTranLogMaxTime() == null) {
				l = 1446307200000L;
			} else {
				l = tradeService.payTranLogMaxTime();
			}
			HashMap<String, String> opts = new HashMap<String, String>();
			opts.put("mid", "100100210000038");
			opts.put("lastReceivedTime", l.toString());
			opts.put("count", "10000");
			opts.put("tableName", "pay_tran_log");
			String data = JSONParser.stringify(opts);
			String url = "http://cashier.91huishang.com//wizarposOpen-server/v1_0/saleData/getdata";

			String json = HttpClientUtil.post(url, data);
			@SuppressWarnings("unchecked")
			HashMap<String, Object> hashmap = (HashMap<String, Object>) JSONParser
					.parse(json, new HashMap<String, Object>().getClass());
			@SuppressWarnings("unchecked")
			List<HashMap<String, Object>> list = (List<HashMap<String, Object>>) hashmap
					.get("payTranLogList");
			for (HashMap<String, Object> map : list) {
				PayTranLog payTranLog = new PayTranLog();
				payTranLog.setId(map.get("id").toString().trim());
				payTranLog.setSn(map.get("sn").toString().trim());
				payTranLog.setTranTime(map.get("tran_time").toString().trim());
				payTranLog.setLastTime(NumberUtils.toLong(map.get("last_time").toString().trim()));
				payTranLog.setTranAmount(NumberUtils.toDouble(map.get("tran_amount").toString().trim())/100);
				payTranLog.setExtraAmount(NumberUtils.toDouble(map.get("extra_amount").toString().trim())/100);
				payTranLog.setInputAmount(NumberUtils.toDouble(map.get("input_amount").toString().trim())/100);
				payTranLog.setCardNo(NumberUtils.toLong(StringUtil
						.checkString((String) map.get("card_no")).toString()
						.trim()));
				payTranLog.setTranCode(NumberUtils.toInt(map.get("tran_code").toString().trim()));
				payTranLog.setTranDesc(map.get("tran_desc").toString().trim());
				payTranLog.setMasterTranLogId(map.get("master_tran_log_id").toString().trim());
				payTranLog.setMixedTranFlag(StringUtil
						.checkString((String) map.get("mixed_tran_flag")).toString()
						.trim());
				tradeService.insertPayTranLog(payTranLog);
			}
		} catch (Exception e) {
			System.out.println(e);
		}
		
		// 写入odb订单trade表
		List<Trade> lists = tradeService.findTradeListTwo();
		List<String> s = tradeService.findTradeIdList();
		for (Trade t : lists) {
			if (!s.contains(t.getOuterId())) {
				Trade trade = new Trade();
				trade.setOuterId(t.getOuterId());
				trade.setOrderSn(t.getOrderSn());
				trade.setGoodsAmount(t.getGoodsAmount());
				trade.setAddTime(t.getAddTime() / 1000);
				trade.setPaymentTime(t.getPaymentTime() / 1000);
				trade.setOrderState(t.getOrderState());
				trade.setRefundState(t.getRefundState());
				trade.setDisCount(t.getDisCount());
				trade.setDisAmount(t.getDisAmount());
				trade.setOddAmount(t.getOddAmount());
				trade.setOrderAmount(t.getOrderAmount());
				trade.setPayMode(t.getPayMode());
				trade.setStoreId(t.getStoreId());
				trade.setStoreName(t.getStoreName());
				trade.setMerchantName(t.getMerchantName());
				trade.setChannelId(2);
				tradeService.insertTrade(trade);
			}
		}
		System.out.println("订单数据对接完成");
	}
}
